package com.wang.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.mapper.TestGuideMapper;
import com.wang.springbootinit.model.entity.TestGuide;
import com.wang.springbootinit.model.entity.User;
import com.wang.springbootinit.model.param.TestGuideQuery;
import com.wang.springbootinit.service.FileService;
import com.wang.springbootinit.service.TestGuideService;
import com.wang.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 测试指南服务实现类
 */
@Service
@Slf4j
public class TestGuideServiceImpl extends ServiceImpl<TestGuideMapper, TestGuide> implements TestGuideService {

    @Autowired
    private TestGuideMapper testGuideMapper;
    
    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;
    
    @Override
    public boolean publishGuide(String title, String content, MultipartFile file, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        // 处理文件上传
        String videoUrl = null;
        if (file != null) {
            videoUrl = fileService.uploadFile(file);
        }

        TestGuide testGuide = new TestGuide();
        testGuide.setStatus(1);
        testGuide.setTitle(title);
        testGuide.setContent(content);
        testGuide.setVideoUrl(videoUrl);
        testGuide.setPublisherName(loginUser.getUserName());
        testGuide.setCreateTime(LocalDateTime.now());
        testGuide.setUpdateTime(LocalDateTime.now());

        int insert = testGuideMapper.insert(testGuide);
        if (insert > 0) {
            return true;
        }
        throw new BusinessException("添加失败");
    }
    
    @Override
    public void updateGuide(Integer id, String title, String content, MultipartFile file, HttpServletRequest request) {
        // 查询现有指南
        TestGuide guide = testGuideMapper.selectById(id);
        if (guide == null) {
            throw new BusinessException("指南不存在");
        }
        
        // 如果有新文件上传，先删除旧文件再上传新文件
        if (file != null) {
            if (guide.getVideoUrl() != null) {
                fileService.deleteFile(guide.getVideoUrl());
            }
            String videoUrl = fileService.uploadFile(file);
            guide.setVideoUrl(videoUrl);
        }
        
        // 更新基本信息
        guide.setTitle(title);
        guide.setContent(content);
        guide.setUpdateTime(LocalDateTime.now());
        
        // 保存更新
        testGuideMapper.updateById(guide);
    }
    
    @Override
    public void offlineGuide(Long id) {
        TestGuide guide = new TestGuide();
        guide.setId(id);
        guide.setStatus(2);
        guide.setUpdateTime(LocalDateTime.now());
        testGuideMapper.updateById(guide);
    }
    
    @Override
    public void deleteGuide(Long id) {
        TestGuide guide = testGuideMapper.selectById(id);
        if(guide != null && guide.getVideoUrl() != null) {
            // 删除视频文件
            fileService.deleteFile(guide.getVideoUrl());
        }
        testGuideMapper.deleteById(id);
    }
    
    @Override
    public Page<TestGuide> pageGuides(TestGuideQuery query) {
        LambdaQueryWrapper<TestGuide> wrapper = new LambdaQueryWrapper<>();
        Page<TestGuide> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<TestGuide> testGuidePage = testGuideMapper.selectPage(page, wrapper);
        return testGuidePage;
    }
    
    @Override
    public TestGuide getGuideDetail(Long id) {
        return testGuideMapper.selectById(id);
    }
    
    @Override
    public String uploadVideo(MultipartFile file) {
        // 上传视频文件
        return fileService.uploadFile(file);
    }
}